package top.frsf.object.zombie;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import top.frsf.action.TimeAttackAble;
import top.frsf.action.TimeMoveAble;
import top.frsf.constants.ImageConstant;
import top.frsf.enums.GameStateEnum;
import top.frsf.enums.ZomStateEnum;
import top.frsf.object.AnimatedGameObject;
import top.frsf.object.GameObject;
import top.frsf.object.plant.PlantObject;
import top.frsf.panel.GamePanel;
import top.frsf.starter.impl.PlantStarter;
import top.frsf.starter.impl.ZomStarter;

import java.awt.*;

import static top.frsf.panel.GamePanel.starterContext;

/**
 * zom基类
 *
 * @author FRSF
 * @since 2025/8/3 12:49
 */
@Slf4j
@Getter
@Setter
public abstract class ZomObject extends AnimatedGameObject implements TimeMoveAble, TimeAttackAble {
    /* base */
    /**
     * 生命值
     */
    protected int hp;
    /**
     * 移动速度
     */
    protected int speed;
    /**
     * 减速速度
     */
    protected int snowSpeed;
    /**
     * 攻击力
     */
    protected int attackHp;
    /**
     * 状态
     */
    protected ZomStateEnum state = ZomStateEnum.MOVE;


    /**
     * 构造函数
     *
     * @param img      图片路径
     * @param x        x坐标
     * @param y        y坐标
     * @param width    宽度
     * @param height   高度
     * @param hp       生命值
     * @param speed    移动速度
     * @param attackHp 攻击力
     */
    public ZomObject(String img, int x, int y, int width, int height, int hp, int speed, int attackHp) {
        super(img, x, y, width, height);
        this.hp = hp;
        this.speed = speed;
        this.attackHp = attackHp;
    }

    @Override
    public void paintSelfImage(Graphics g) {
        super.paintSelfImage(g);
        String info = String.format("HP: %s, Speed：%s", hp, getFinalSpeed());
        g.setColor(Color.RED);
        g.drawString(info, getX() + 5, getY() - 10);
        if (GamePanel.state == GameStateEnum.RUNNING) {
            PlantStarter plantStarter = starterContext.getStarter(PlantStarter.class);
            for (PlantObject plant : plantStarter.getPlants()) {
                Rectangle plantRectangle = plant.getRectangle();
                if (getRectangle().contains(plantRectangle.getCenterX(), plantRectangle.getCenterY())) {
                    // 该植物处于zom攻击范围，开启攻击模式
                    state = ZomStateEnum.ATTACK;
                    switchAnimation(ImageConstant.Zom.ZOM_ATTACK, 100, true);
                    attack(plant);
                    return;
                }
            }

            if (state == ZomStateEnum.SNOW_MOVE) {
                switchAnimation(ImageConstant.Zom.ZOM_SNOW_MOVE, 100, true);
            } else {
                switchAnimation(ImageConstant.Zom.ZOM_MOVE, 50, true);
            }
            move();
        }

    }

    @Override
    public void performMove() {
        setX(getX() - getFinalSpeed());
    }

    @Override
    public void performAttack(GameObject target) {
        PlantObject plant = (PlantObject) target;
        plant.downHp(attackHp);
    }

    /**
     * 获取最终移动速度
     *
     * @return 最终移动速度
     */
    public int getFinalSpeed() {
        return this.snowSpeed == 0 ? this.speed : this.snowSpeed;
    }

    /**
     * 减血
     *
     * @param hp 减血数
     */
    public void downHp(int hp) {
        this.hp -= hp;
        if (this.hp <= 0) {
            ZomStarter zomStarter = starterContext.getStarter(ZomStarter.class);
            zomStarter.getDeadZomList().add(this);
        }
    }
}
